﻿using System;
using System.Text;
using System.Data;
using System.Data.Common;
using System.Collections.Generic;
using System.Diagnostics;

namespace EventScavenger
{

    public class PollFilter
    {
        public PollFilter()
        {
            PollFilterID = -1;
            MessageContains = "";
            IncludeEventIDs = new List<int>();
            ExcludeEventIDs = new List<int>();
            IncludeSources = new List<string>();
            ExcludeSources = new List<string>();
            IncludeSourceIDs = new List<int>();
            ExcludeSourceIDs = new List<int>();
            UseListFilters = false;
            LastChanged = null;
        }
        public int PollFilterID { get; set; }
        public string Name { get; set; }
        public string MessageContains { get; set; }
        public DateTime? LastChanged { get; set; }
        public bool ExcludeInfo { get; set; }
        public bool ExcludeWarn { get; set; }
        public bool ExcludeErr { get; set; }
        public bool ExcludeSucAud { get; set; }
        public bool ExcludeFailAud { get; set; }
        public bool UseListFilters { get; set; }

        public List<int> IncludeEventIDs { get; internal set; }
        public List<int> ExcludeEventIDs { get; internal set; }
        public List<string> IncludeSources { get; internal set; }
        public List<string> ExcludeSources { get; internal set; }
        public List<int> IncludeSourceIDs { get; internal set; }
        public List<int> ExcludeSourceIDs { get; internal set; }

        public bool IsInFilter(string message, int eventId, string source)
        {
            if (ExcludeEventIDs.Count > 0)
                if (ExcludeEventIDs.Contains(eventId))
                    return false;
            if (IncludeEventIDs.Count > 0)
                if (!IncludeEventIDs.Contains(eventId))
                    return false;
            if (ExcludeSources.Count > 0)
                if (ExcludeSources.Contains(source))
                    return false;
            if (IncludeSources.Count > 0)
                if (!IncludeSources.Contains(source))
                    return false;
            if (MessageContains.Length > 0)
                if (!message.Contains(MessageContains))
                    return false;

            return true;
        }

        public bool IsEventLogEntryTypeInFilter(EventLogEntryType entryType)
        {
            if (ExcludeFailAud && entryType == EventLogEntryType.FailureAudit)
                return false;
            else if (ExcludeInfo && entryType == EventLogEntryType.Information)
                return false;
            else if (ExcludeWarn && entryType == EventLogEntryType.Warning)
                return false;
            else if (ExcludeErr && entryType == EventLogEntryType.Error)
                return false;
            else if (ExcludeSucAud && entryType == EventLogEntryType.SuccessAudit)
                return false;            
            else
                return true;
        }

        public override string ToString()
        {
            return Name;
        }
    }
}
